package com.lss.myapp.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lss.myapp.R;
import com.lss.myapp.constant.AppConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private EditText btnRegisterAccount;
    private EditText btnRegisterPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        findViewById(R.id.btn_register).setOnClickListener(this);
        btnRegisterAccount = findViewById(R.id.btn_register_account);
        btnRegisterPwd = findViewById(R.id.btn_register_pwd);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_register) {
            String account = btnRegisterAccount.getText().toString().trim();
            String pwd = btnRegisterPwd.getText().toString().trim();
            if (account.isEmpty()) {
                showToast("请输入账户");
                return;
            }
            if (pwd.isEmpty()) {
                showToast("请输入密码");
                return;
            }
            // okhttp3 account pwd post请求
            // 使用 OkHttp3 发送 POST 请求
            OkHttpClient client = new OkHttpClient();

            // 构建 JSON 请求体
            MediaType JSON = MediaType.get("application/json; charset=utf-8");
            JSONObject jsonBody = new JSONObject();
            try {
                jsonBody.put("mobile", account);
                jsonBody.put("password", pwd);  // 实际使用中建议加密后再传输
            } catch (JSONException e) {
                e.printStackTrace();
            }

            RequestBody requestBody = RequestBody.create(jsonBody.toString(), JSON);
            // 构建请求体
//            RequestBody requestBody = new FormBody.Builder()
//                    .add("mobile", account)
//                    .add("password", pwd)  // 注意：实际项目中应加密传输密码
//                    .build();

            // 构建请求
            Request request = new Request.Builder()
                    .url(AppConfig.BASE_URL + "/app/register")  // 替换为你的登录接口地址
                    .post(requestBody)
                    .build();

            // 异步请求
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    runOnUiThread(() -> showToast("网络请求失败: " + e.getMessage()));
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String responseData = response.body().string();
                        // 解决ui线程阻塞的问题
                        runOnUiThread(() -> {
                            showToast("注册成功");
                            // TODO: 处理登录成功后的跳转或其他逻辑
                        });
                    } else {
                        runOnUiThread(() -> showToast("注册失败，请检查账户或密码" + response));
                        Log.i("lss-test", String.valueOf(response));
                    }
                }
            });
        }
    }
}